home *** CD-ROM | disk | FTP | other *** search
- Path: frco.com!usenet
- From: Jadam@tcmail.frco.com (Jim Adam)
- Newsgroups: comp.lang.c++
- Subject: Re: should operator== and operator= be virtual?
- Date: 5 Feb 1996 19:58:33 GMT
- Organization: Fisher Rosemount Systems
- Message-ID: <4f5nh9$e5p@rolaids.frco.com>
- References: <4ejvkr$btb@itnews.sc.intel.com>
- NNTP-Posting-Host: primrose.frco.com
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.11
-
- In article <4ejvkr$btb@itnews.sc.intel.com>, etse@scdt.intel.com says...
-
- >Should operator==() and operator=() of a class be virtual if the class is
- >planned for extensibility? Personally I think in general assignement
- >operators should be non-virtual, but I am not sure about equality operator.
- >Could anyone helps? Thanks.
-
-
- >class A {
- >public:
- > A& operator=(const A&);
- > virtual bool operator==(const A&) const;
- > virtual bool operator!=(const A&) const;
- >};
-
-
- >class B : public A {
- >public:
- > B& operator=(const B&);
- >
- > virtual bool operator==(const A&) const; // ?
- > virtual bool operator!=(const A&) const; // ?
- >
- > virtual bool operator==(const B&) const; // ?
- > virtual bool operator!=(const B&) const; // ?
-
-
- I think the virtual operator==() rarely makes sense. It implies
- either (a) that class B will use a different mechanism to compare
- the value of inherited member variables than class A would or
- (b) you consider it essential to always fully compare two B's
- together, and inside the operator==( const A&) you're going to
- do run-time type checking to determine if the argument is a B.
- If it is a B, you'll call the operator==( const B&) function.
- If not (maybe it's a C), you'll let the base class's equality
- operator handle the problem.
-
- --The real question, tho, is under what circumstances any of this
- makes sense. There, I think you have to decide based on your
- particular situation. For me, I don't think I'd ever use it.
- If I do, say:
-
- list<A> somelist.
- // ADD As, Bs, Cs, etc., to somelist
- somelist.sort();
-
- I expect the operator<() and operator==() defined on A to
- suffice. Otherwise, maybe it's in the compare functor I
- pass to the sort function that all the RTTI stuff gets done,
- and not via virtual compare functions inside the classes.
-
- Jim
-
-